//
//  GHAddCardViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/16.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHAddCardViewController.h"
#define bankeDes @"请选择银行卡"
@interface GHAddCardViewController ()
@property(nonatomic, strong) UITextField                     *zhihangTextField;
@property(nonatomic, strong) UITextField                     *nameTextField;
@property(nonatomic, strong) UITextField                     *idCardTextField;
@property(nonatomic, strong) STButton                        *bankButton;//银行
@property(nonatomic, strong) NSArray                         *bankArray;

@property(nonatomic, strong) NSArray <GHTableEasyModel*>                    *datasouce;
@end

@implementation GHAddCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加银行卡";
    [self st_setRightItemWithTitle:@"保存" titleColor:[UIColor whiteColor]];
    [self configSubView];
    [self sendBanksRequst];
    self.datasouce = @[
                       [[GHTableEasyModel alloc]initWithTextString:@"提现支行" accessoryView:self.zhihangTextField],
                       [[GHTableEasyModel alloc]initWithTextString:@"提现姓名" accessoryView:self.nameTextField],
                       [[GHTableEasyModel alloc]initWithTextString:@"提现账号" accessoryView:self.idCardTextField]
                       ];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    _bankButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 50)
                                                     title:@"请选择银行卡"
                                                titleColor:GH_firstTextColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:[UIColor whiteColor]
                                           backgroundImage:nil
                                                     image:nil];
    [self.bankButton setTitleEdgeInsets:UIEdgeInsetsMake(-5, 15, 0, 0)];
    [self.bankButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedBankTypeButton];
    }];
    self.tableView.tableHeaderView = weakSelf.bankButton;
    
    self.zhihangTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)];
    self.zhihangTextField.keyboardType = UIKeyboardTypeDefault;
    self.zhihangTextField.placeholder = @"xx省xx市xx区xx支行";
    self.zhihangTextField.textColor = GH_firstTextColor;
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)];
    self.nameTextField.keyboardType = UIKeyboardTypeDefault;
    self.nameTextField.placeholder = @"姓名";
    self.nameTextField.textColor = GH_firstTextColor;
    
    self.idCardTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)];
    self.idCardTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.idCardTextField.placeholder = @"银行卡卡号";
    self.idCardTextField.textColor = GH_firstTextColor;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datasouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    cell.textLabel.text = [NSString stringWithFormat:@"%lu",indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    cell.textLabel.text = self.datasouce[indexPath.row].textString;
    cell.accessoryView = self.datasouce[indexPath.row].accessoryView;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{
    
    if ([self.bankButton.currentTitle isEqualToString:bankeDes]) {
        [SVProgressHUD showErrorWithStatus:bankeDes];
        return;
    }
    if (!self.zhihangTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入提现支行"];
        return;
    }
    if (!self.nameTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入提现姓名"];
        return;
    }
    if (!self.idCardTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入提现账号"];
        return;
    }
    [self sendAddbankRequest];
    
}
- (void)onSelectedBankTypeButton{
    if (!self.bankArray.count) {
        [self sendBanksRequst];
        return;
    }
    STPickerViewController * vc = [[STPickerViewController alloc] initWithPickerArray:@[self.bankArray] andWithHandle:^(NSArray<NSString *> *stringArray) {
        [self.bankButton setTitle:stringArray.firstObject forState:UIControlStateNormal];
    }];
    [self presentViewController:vc animated:NO completion:nil];
}
//获取银行列表
- (void)sendBanksRequst{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }

    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/cash/get_banks"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSDictionary * dataArray = [responseObject valueForKey:@"data"];
                                                 NSMutableArray *newarrya = [NSMutableArray new];
                                                 [newarrya addObject:bankeDes];
                                                 for (NSDictionary * dataDic in dataArray) {
                                                     if ([dataDic.allKeys containsObject:@"bank"]) {
                                                         NSString * bank = dataDic[@"bank"];
                                                         [newarrya addObject:bank];
                                                     }
                                                 }
                                                 self.bankArray = newarrya;
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 NSLog(@"error:%@",error.desc);
                                             }];
}
- (void)sendAddbankRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [dic setObject:self.bankButton.currentTitle forKey:@"bank_name"];
    
    if (self.zhihangTextField.text.length) {
        [dic setObject:self.zhihangTextField.text forKey:@"branch_name"];
    }
    if (self.nameTextField.text.length) {
        [dic setObject:self.nameTextField.text forKey:@"account_name"];
    }
    if (self.idCardTextField.text.length) {
        [dic setObject:self.idCardTextField.text forKey:@"account_bank"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/cash/add_address"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                  [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"添加成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
